/*
 * @Author: your name
 * @Date: 2021-02-16 12:40:22
 * @LastEditTime: 2021-02-19 00:13:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \demo_2021021601\bsp\at32f421\project\main.c
 */
/*
*********************************************************************************************************
*
*	程序名称 : 综合测试例子
*	文件名称 : main.c
*	版    本 : v1.0.00
*   编    写 : 莫利奖
*   日    期 : 2020年11月30日
*	说    明 : 开发板综合测试，测试led输出，按键输入，串口通讯以及外部时钟。
*
*********************************************************************************************************
*/

#include "bsp.h"

/* 定义例程名和例程发布日期 */
#define EXAMPLE_NAME "AT32F421开发板综合测试"
//#define EXAMPLE_NAME "AT32F421board demo test"
//#define EXAMPLE_DATE "2020年11月30日"
#define EXAMPLE_DATE "2/17/2020"
#define DEMO_VER "v1.0.00"

static void PrintfLogo(void);

extern volatile uint8_t ucSys10msFlag;

/*
*********************************************************************************************************
*	函 数 名: main
*	功能说明: 标准c程序入口
*	形    参: 无
*	返 回 值: 无
*********************************************************************************************************
*/
int main(void)
{
    ERTC_DateType ERTC_DateStructure;
    ERTC_TimeType ERTC_TimeStructure;
    uint8_t ucSecondsOld = 0;
    uint8_t ucKeyData = 0;
    uint32_t data2 = 2;
    uint64_t data = 0;
    /* 系统初始化 */
    System_Init();
    /* 板子信息 */
    PrintfLogo();
    data = (uint64_t)data2 * 1000 / 5;
    my_printf("data is %d\r\n", (uint32_t)data);

    while (1)
    {
        /* 系统10ms时基 */
        if (ucSys10msFlag)
        //if (1)
        {
            ucSys10msFlag = 0;
            /* 按键驱动 */
            ucKeyData = Key_Driver();
            if (ucKeyData == S_KEY)
            {
                //my_printf("->按键单击\r\n");
                my_printf("->key click on\r\n");
            }
            else if (ucKeyData == L_KEY)
            {
                //my_printf("->按键长按\r\n");
                my_printf("->key long click on\r\n");
            }
            /* 实时时钟 */
            /* 读取日期 */
            ERTC_GetDateValue(ERTC_Format_BIN, &ERTC_DateStructure);
            /* 读取日时钟 */
            ERTC_GetTimeValue(ERTC_Format_BIN, &ERTC_TimeStructure);
            /* 如果秒变化则刷新 */
            if (ucSecondsOld != ERTC_TimeStructure.ERTC_Seconds)
            {
                ucSecondsOld = ERTC_TimeStructure.ERTC_Seconds;
                /* 打印 */
                //my_printf("->20%0.2d年%0.2d月%0.2d日  %0.2d时%0.2d分%0.2d秒\r\n", ERTC_DateStructure.ERTC_Year, ERTC_DateStructure.ERTC_Month, ERTC_DateStructure.ERTC_Date,\
                          ERTC_TimeStructure.ERTC_Hours, ERTC_TimeStructure.ERTC_Minutes, ERTC_TimeStructure.ERTC_Seconds);
                my_printf("->20%0.2dyear%0.2dmon%0.2dday %0.2dhour%0.2dminute %0.2dsecond\r\n", ERTC_DateStructure.ERTC_Year, ERTC_DateStructure.ERTC_Month, ERTC_DateStructure.ERTC_Date,
                          ERTC_TimeStructure.ERTC_Hours, ERTC_TimeStructure.ERTC_Minutes, ERTC_TimeStructure.ERTC_Seconds);
                /* LED翻转 */
                bsp_LedToggle();
            }
        }
    }
}

/*
*********************************************************************************************************
*	函 数 名: PrintfLogo
*	功能说明: 打印例程名称和例程发布日期
*	形    参: 无
*	返 回 值: 无
*********************************************************************************************************
*/
static void PrintfLogo(void)
{
    uint32_t CPU_Sn0, CPU_Sn1, CPU_Sn2;
    RCC_ClockType RCC_ClocksStatus;
    RCC_GetClocksFreq(&RCC_ClocksStatus);
    my_printf("-------------------------------------------------------------\n\r");
    my_printf("->demo name %s\r\n", EXAMPLE_NAME);  /* 打印例程名称 */
    my_printf("->demo version %s\r\n", DEMO_VER);   /* 打印例程版本 */
    my_printf("->date issue %s\r\n", EXAMPLE_DATE); /* 打印例程日期 */

    /* UID */
    CPU_Sn0 = *(__IO uint32_t *)(0x1FFFF7E8);
    CPU_Sn1 = *(__IO uint32_t *)(0x1FFFF7E8 + 4);
    CPU_Sn2 = *(__IO uint32_t *)(0x1FFFF7E8 + 8);

    my_printf("->CPU: AT32F421C8T7, LQFP48, Basic Frequency: %dMHz\r\n", SystemCoreClock / 1000000);

    my_printf("SYSCLK_Freq is %d\r\n", RCC_ClocksStatus.SYSCLK_Freq);
    my_printf("AHBCLK_Freq is %d\r\n", RCC_ClocksStatus.AHBCLK_Freq);
    my_printf("APB1CLK_Freq is %d\r\n", RCC_ClocksStatus.APB1CLK_Freq);
    my_printf("APB2CLK_Freq is %d\r\n", RCC_ClocksStatus.APB2CLK_Freq);
    my_printf("ADCCLK_Freq is %d\r\n", RCC_ClocksStatus.ADCCLK_Freq);
    my_printf("TMR6_GLOBAL_IRQn is %d\r\n", TMR6_GLOBAL_IRQn);

    my_printf("->FLASH: 64Kbyte, SRAM: 16Kbyte\r\n");
    my_printf("->UID: %08X %08X %08X\n\r", CPU_Sn2, CPU_Sn1, CPU_Sn0);

    /* 库版本 */
    my_printf("->Library version: V1.2.7\r\n");
    my_printf("-------------------------------------------------------------\n\r");
}

#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
     ex: my_printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}
#endif

/********************************************* End of file **********************************************/
